package com.lys.demo.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import com.fasterxml.jackson.annotation.JsonView;
import com.lys.demo.dto.UserCondition;
import com.lys.demo.dto.UserInfo;
import com.lys.demo.dto.UserInfo.UserDetailView;
import com.lys.demo.dto.UserInfo.UserListView;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 1. 页面跳转？
 * 
 * @author liyinsong
 *
 */

@RestController
//@RequestMapping
public class DemoSearchController {

	// @GetMapping
	@RequestMapping(value = "/user", method = RequestMethod.GET)
	@JsonView(UserListView.class)
	@ApiOperation("查询用户信息")
	public List<UserInfo> query() {
		throw new RuntimeException("内部错误");
//		List users = new ArrayList<UserInfo>();
//		users.add(new UserInfo());
//		users.add(new UserInfo());
//		users.add(new UserInfo());
//
//		return users;
	}

	@RequestMapping(value = "/users", method = RequestMethod.GET)
	@JsonView(UserListView.class)
	// public List<UserInfo> query(String name) {
	// public List<UserInfo> query(@RequestParam(value = "name") String
	// userName) {
	// Pageable @PageableDefault
	public List<UserInfo> query(UserCondition userCondition) {
		System.out.println(userCondition.getId());
		System.out.println(userCondition.getName());

		List users = new ArrayList<UserInfo>();
		users.add(new UserInfo());
		users.add(new UserInfo());
		users.add(new UserInfo());

		return users;
	}

	//@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
	@RequestMapping(value = "/users/{id:\\d}", method = RequestMethod.GET)
	@JsonView(UserDetailView.class)
	@ApiOperation("查询用户详细信息")
	//public UserInfo getDetail(@PathVariable long id) {
	public UserInfo getDetail(@ApiParam("用户ID") @PathVariable long id, @CookieValue String token, @RequestHeader String auth) {
		System.out.println(id);
		System.out.println(token);
		System.out.println(auth);
		UserInfo user = new UserInfo();
		user.setName("andy");
		user.setId(id);
		user.setCreateDate(new Date());
		return user;
	}
	//异步处理请求: 1) 只有一个callable线程，所有处理这个线程都能完成
	/*
	@RequestMapping(value = "/users/{id:\\d}", method = RequestMethod.GET)
	@JsonView(UserDetailView.class)
	public Callable<UserInfo> getDetail(@PathVariable long id) {
		System.out.println(Thread.currentThread().getName() + "开始");
		
		Callable<UserInfo> result = () -> {
			System.out.println(Thread.currentThread().getName() + "线程开始");
			Thread.sleep(2000);
			UserInfo user = new UserInfo();
			user.setName("andy");
			user.setId(id);
			user.setCreateDate(new Date());
			System.out.println(Thread.currentThread().getName() + "线程结束");
			return user;
		};
		
		
		System.out.println(Thread.currentThread().getName() + "结束");
		return result;
	}
	*/
	//异步处理请求: 2) 有一个callable线程，还有另外一个线程，第一个线程发请求、第二个线程监听结果。这里由两个线程完成一个动作
	/*
	private ConcurrentMap<Long, DeferredResult<UserInfo>> map = new ConcurrentHashMap<>();
	
	@RequestMapping(value = "/users/{id:\\d}", method = RequestMethod.GET)
	@JsonView(UserDetailView.class)
	public DeferredResult<UserInfo> getDetail(@PathVariable long id) {
		System.out.println(Thread.currentThread().getName() + "开始");
		
		DeferredResult<UserInfo> result = new DeferredResult<>();
		map.put(id, result);
		
		System.out.println(Thread.currentThread().getName() + "结束");
		return result;
	}
	
	private void listenMessage(UserInfo userInfo) {
		map.get(userInfo.getId()).setResult(userInfo);
	}
	*/
	
	
	@PostMapping("/users")
	public UserInfo create(@Valid @RequestBody UserInfo user, BindingResult result) {
		if (result.hasErrors()) {
			//result.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
			result.getAllErrors().stream().forEach(error -> System.out.println(error.getCode()));
		}
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getPassword());
		System.out.println(user.getCreateDate());
		user.setId(1l);
		return user;
	}
	
	@PutMapping("/users/{id}")
	public UserInfo update(@Valid @RequestBody UserInfo user) {
		System.out.println(user.getId());
		System.out.println(user.getName());
		System.out.println(user.getPassword());
		System.out.println(user.getCreateDate());
		System.out.println("");
		user.setId(1l);
		return user;
	}
	
	@DeleteMapping("/users/{id}")
	public void delete(@PathVariable long id) {
		
		System.out.println(id);
	}
}
